<template>
  <div class="settings-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-6 offset-md-3 col-xs-12">
          <h1 class="text-xs-center">Your Settings</h1>

          <form @submit.prevent="onSubmit">
            <fieldset>
              <fieldset class="form-group">
                <input
                  :disbaled="loading"
                  v-model="info.image"
                  class="form-control"
                  type="text"
                  placeholder="URL of profile picture"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  :disbaled="loading"
                  v-model="info.username"
                  class="form-control form-control-lg"
                  type="text"
                  placeholder="Your Name"
                />
              </fieldset>
              <fieldset class="form-group">
                <textarea
                  :disbaled="loading"
                  v-model="info.bio"
                  class="form-control form-control-lg"
                  rows="8"
                  placeholder="Short bio about you"
                ></textarea>
              </fieldset>
              <fieldset class="form-group">
                <input
                  :disbaled="loading"
                  v-model="info.email"
                  class="form-control form-control-lg"
                  type="email"
                  placeholder="Email"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  :disbaled="loading"
                  v-model="info.password"
                  class="form-control form-control-lg"
                  type="password"
                  placeholder="Password"
                />
              </fieldset>
              <button
                :disbaled="loading"
                class="btn btn-lg btn-primary pull-xs-right"
              >
                Update Settings
              </button>
            </fieldset>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { updateUser } from "@/api/profile";
export default {
  name: "settings",
  data() {
    return {
      loading: false,
      info: {
        image: "",
        bio: "",
        username: "",
        email: "",
        password: "",
      },
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  mounted() {
    this.info = {
      image: this.user.iamge,
      bio: this.user.bio,
      username: this.user.username,
      email: this.user.email,
      password: "",
    };
  },
  methods: {
    async onSubmit() {
      this.loading = true;
      const { data } = await updateUser({ user: this.info });
      this.loading = false;
      this.$router.push(`/profile/${data.user.username}`);
    },
  },
};
</script>
